package com.yugao.fintech.antelope.pay.common.aspect;

import com.yugao.fintech.antelope.pay.common.annotation.PayCheck;
import com.yugao.fintech.antelope.pay.common.constants.Cs;
import com.yugao.fintech.antelope.pay.model.entity.MchApp;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.AbstractMchAppRQ;
import com.yugao.fintech.antelope.pay.payment.model.bo.MchAppConfigContext;
import com.yugao.fintech.antelope.pay.payment.service.ConfigContextQueryService;
import com.yugao.fintech.framework.assistant.utils.CollectionUtils;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 数据过滤处理
 * 

 */
@Slf4j
@Aspect
@Component
public class PayCheckAspect {
    @Autowired
    protected ConfigContextQueryService configContextQueryService;
    @Autowired
    protected HttpServletRequest request;

    /**
     * 处理完请求后执行
     */
    @Around("@annotation(payCheck)")
    public Object around(ProceedingJoinPoint point, PayCheck payCheck) throws Throwable {
        // 参数值
        Object[] argsValue = point.getArgs();
        List<Object> reqObjList = Arrays.stream(argsValue).filter(e -> e instanceof AbstractMchAppRQ)
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(reqObjList)) {
            log.warn("request url {} 请求对象没有继承于 {}", request.getRequestURI(), AbstractMchAppRQ.class.getSimpleName());
        } else {
            // 请求签名
            Object reqObj = reqObjList.stream().findFirst()
                    .orElseThrow(() -> new BizException("未查找到入参继承于 {}", AbstractMchAppRQ.class.getSimpleName()));
            preCheck((AbstractMchAppRQ) reqObj);

        }

        // 如果没有使能签名校验直接返回
        return point.proceed();
    }

    protected MchAppConfigContext preCheck(AbstractMchAppRQ rq) {
        String mchNo = rq.getMchNo();
        String appId = rq.getAppId();
        if(StringUtils.isAnyBlank(mchNo, appId)){
            throw new BizException("参数有误！");
        }

        MchAppConfigContext mchAppConfigContext = configContextQueryService.queryMchInfoAndAppInfo(mchNo, appId);

        if(mchAppConfigContext == null){
            throw new BizException("商户或商户应用不存在");
        }

        if(mchAppConfigContext.getMchInfo() == null || !Objects.equals(mchAppConfigContext.getMchInfo().getStatus(), Cs.YES)){
            throw new BizException("商户信息不存在或商户状态不可用");
        }

        MchApp mchApp = mchAppConfigContext.getMchApp();
        if(mchApp == null || !Cs.YES.equals(mchApp.getStatus())){
            throw new BizException("商户应用不存在或应用状态不可用");
        }

        if(!mchApp.getMchNo().equals(mchNo)){
            throw new BizException("参数appId与商户号不匹配");
        }
        return mchAppConfigContext;
    }
}
